package com.mrjy.test.bo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.CoursePackageVip;
import com.mrjy.common.entity.UserMemberInfo;
import com.mrjy.common.entity.UserTest;
import com.mrjy.coursepackage.dao.ICoursePackageInfoDao;
import com.mrjy.coursepackage.dao.ICoursePackageVipDao;
import com.mrjy.coursepackage.dto.CoursePackageInfoDTO;
import com.mrjy.maintain.dao.IUserMemberInfoDao;
import com.mrjy.test.dao.IUserTestDao;

@Service
public class UserTestBo implements IUserTestBo{
	
	@Resource
	private IUserTestDao userTestDao;
	@Resource
	private ICoursePackageInfoDao coursePackageInfoDao;
	@Resource
	private IUserMemberInfoDao userMemberInfoDao;
	@Resource
	private ICoursePackageVipDao coursePackageVipDao;
	
	@Override
	public Object txInsert(UserTest userTest) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (userTest.getUserId()==null||userTest.getStageId()==null||userTest.getCharacterId()==null
				||userTest.getChildrenId()==null||userTest.getWantToKnowId()==null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		String characterStr = "";
		String childrenStr = "";
		String wantToKnowStr = "";
		if (userTest.getCharacterId()!=null && userTest.getCharacterId().length>0) {
			for (Integer characterId : userTest.getCharacterId()) {
				characterStr += characterId+",";
			}
		}
		if (userTest.getChildrenId()!=null && userTest.getChildrenId().length>0) {
			for (Integer childrenId : userTest.getChildrenId()) {
				childrenStr += childrenId+",";
			}
		}
		if (userTest.getWantToKnowId()!=null && userTest.getWantToKnowId().length>0) {
			for (Integer wantToKnowId : userTest.getWantToKnowId()) {
				wantToKnowStr += wantToKnowId+",";
			}
		}
		userTest.setStage(userTest.getStageId()+"");
		userTest.setCharacter(characterStr.substring(0, characterStr.length()-1));
		userTest.setChildren(childrenStr.substring(0, childrenStr.length()-1));
		userTest.setWantToKnow(wantToKnowStr.substring(0, wantToKnowStr.length()-1));
		//查询用户是否已有测试结果
		UserTest test = userTestDao.queryByUserId(userTest);
		if (test!=null) {  //已有测试结果,则修改结果
			int row = userTestDao.updateByUserId(userTest);
			if (row > 0) {
				map.put("err_code", 0);
				map.put("err_msg", "更新成功");
			}else {
				map.put("err_code", 1);
				map.put("err_msg", "更新失败");
			}
			return map;
		}
		//没有测试过, 新增
		int row = userTestDao.insert(userTest);
		if (row > 0) {
			map.put("err_code", 0);
			map.put("err_msg", "添加成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "添加失败");
		}
		return map;
	}

	@Override
	public Object queryByTestResult(UserTest userTest) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<CoursePackageInfoDTO> list = new ArrayList<>();
		if (userTest.getUserId() == null) {
			map.put("err_code", 400);
			map.put("err_msg", "参数异常");
			return map;
		}
		/* 判断用户是否是会员 */
		UserMemberInfo userMemberInfo = userMemberInfoDao.selectIsVipByUserId(userTest.getUserId());
		
		userTest.setHasTest("yes");
		list = coursePackageInfoDao.queryCpByTestResult(userTest);
		if (userMemberInfo!=null) {
			map.put("code", 0);
			map.put("msg", "用户是会员");
		}else {
			map.put("code", 1);
			map.put("msg", "用户非会员");
			//一次查出所有测试结果,再判断是否全部购买
			int payNum = 0;
			UserTest vo = new UserTest();
			vo.setHasTest("yes");
			vo.setUserId(userTest.getUserId());
			List<CoursePackageInfoDTO> all = coursePackageInfoDao.queryCpByTestResult(vo);
			for (CoursePackageInfoDTO dto : all) {
				CoursePackageVip vip = new CoursePackageVip();
				vip.setCoursePackageId(dto.getId());
				vip.setUserId(userTest.getUserId());
				vip.setIsVipStatus(1);
				vip.setDisableFlag(0);
				List<CoursePackageVip> coursePackageVip = coursePackageVipDao.isCoursePackageVip(vip);
				if (coursePackageVip!=null && coursePackageVip.size()>0) { //购买了该课程包且未到期
					payNum++;
					for (CoursePackageInfoDTO resultDto : list) {
						if (dto.getId()==resultDto.getId()) {
							resultDto.setIsVipStatus(true);
						}
					}
				}
			}
			if (payNum != 0 && payNum == all.size()) { //用户购买了测试结果推荐的所有课程包
				map.put("payAll", "yes");
			}else {
				map.put("payAll", "no");
			}
		}
			
		if (list!=null&&list.size()>0) {
			map.put("err_code", 0);
			map.put("err_msg", "有数据");
			map.put("coursepackageList", list);
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "无数据");
		}
		return map;
	}

	@Override
	public Object queryLearningPlan(UserTest userTest) {
		Map<String, Object> map = new HashMap<>();
		List<CoursePackageInfoDTO> list = null;
		/* 判断用户是否是会员 */
		UserMemberInfo userMemberInfo = userMemberInfoDao.selectIsVipByUserId(userTest.getUserId());
		/* 判断用户是否测试过 */
		UserTest test = userTestDao.queryByUserId(userTest);
		
		if (userMemberInfo==null) {  //非会员,展示已购买的cp
			CoursePackageVip vip = new CoursePackageVip();
			vip.setPage(userTest.getPage());
			vip.setRows(userTest.getRows());
			vip.setUserId(userTest.getUserId());
			int count = coursePackageVipDao.queryCountUserPaidCPByUserid(vip);
			list = coursePackageVipDao.queryUserPaidCPByUserid(vip);
			if (test!=null) {  //测试过
				map.put("code", 1);
				map.put("msg", "用户非会员,测试过,展示已购买的课程包");
			}else {
				if (count>0) {  //购买过
					map.put("code", 3);
					map.put("msg", "用户非会员,未测试,展示已购买的课程包");
				}else {
					map.put("code", 4);
					map.put("msg", "用户非会员,未测试,未购买任何课程包");
				}
			}
			
		}else {	//是会员
			if (test==null) { //未测试过,默认排序展示
				userTest.setHasTest("no");
				list = coursePackageInfoDao.queryCpByTestResult(userTest);
				map.put("code", 2);
				map.put("msg", "用户是会员,未测试过,展示默认排序的课程包");
			}else {  //测试过,按测试结果展示
				userTest.setHasTest("yes");
				list = coursePackageInfoDao.queryCpByTestResult(userTest);
				map.put("code", 0);
				map.put("msg", "用户是会员,测试过,展示测试结果");
			}
		}
		map.put("coursepackageList", list);
		return map;
	}
}
